﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;
using KPIS.GERP.GHRMS.DAL;
using KPIS.GERP.GHRMS.IDAL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.DALFactory;

namespace KPIS.GERP.GHRMS.BLL
{
    public sealed class JobSiteBLL : IJobSiteBLL
    {
        private static readonly IJobSiteDAL dal = DataAccess.CreateJobSites();        

        private JobSiteInfo jobsiteInfo = new JobSiteInfo();
        private JobSiteTypeInfo typeInfo = new JobSiteTypeInfo();
        private JobSiteLevelInfo levelInfo = new JobSiteLevelInfo();
        private WorkAreaInfo areaInfo = new WorkAreaInfo();
        private MasDistrictInfo disInfo = new MasDistrictInfo();
        private MasCityInfo cityInfo = new MasCityInfo();
        private MasProvinceInfo provInfo = new MasProvinceInfo();
        private MasCountryInfo couInfo = new MasCountryInfo();

        public DataTable ListJobSite(string strJobSiteTypeSeq)
        {
            return dal.ListJobSite(strJobSiteTypeSeq);
        }

        public IList<JobSiteInfo> ListJobSiteByTypes(string JobSiteTypes)
        {
            if (JobSiteTypes.Length > 0)
                typeInfo.Sequence = Int32.Parse(JobSiteTypes);

            jobsiteInfo.JobSiteTypes = typeInfo;
            return dal.ListJobSite(jobsiteInfo);
        }
        public IList<JobSiteInfo> ListJobSiteByTypes(string JobSiteTypes, string JobSiteLevels)
        {
            if (JobSiteTypes.Length > 0)
                typeInfo.Sequence = Int32.Parse(JobSiteTypes);
            if (JobSiteLevels.Length > 0)
                levelInfo.Sequence = Int32.Parse(JobSiteLevels);

            jobsiteInfo.JobSiteTypes = typeInfo;
            jobsiteInfo.JobSiteLevels = levelInfo;
            return dal.ListJobSite(jobsiteInfo);
        }
        public IList<JobSiteInfo> ListJobSiteByUppers(string UpperSeq)
        {
            if (UpperSeq.Length > 0)
                jobsiteInfo.Upper = Int32.Parse(UpperSeq);
            return dal.ListJobSite(jobsiteInfo);
        }
        public IList<JobSiteInfo> ListJobSiteBySequence(string Sequence)
        {
            if (Sequence.Length > 0)
                jobsiteInfo.Sequence = Int32.Parse(Sequence);
            return dal.ListJobSite(jobsiteInfo);
        }
        public IList<JobSiteInfo> LoadJobSiteTreeView(Nullable<int> JobSiteSeq, Nullable<int> JobSiteTypeSeq)
        {
            return dal.LoadJobSiteTreeView(JobSiteSeq, JobSiteTypeSeq);
        }
        public DataSet LoadJobSites(string Sequence, string Types, string Upper, string Level)
        {
            JobSiteInfo jsInfo = new JobSiteInfo();

            if (Sequence.Length > 0)
                jsInfo.Sequence = Int32.Parse(Sequence);
            if (Upper.Length > 0)
            {
                jsInfo.Upper = Int32.Parse(Upper);
                jsInfo.Sequence = 0;
            }
            if (Types.Length > 0)
            {
                typeInfo.Sequence = Int32.Parse(Types);
                jsInfo.JobSiteTypes = typeInfo;
            }
            if (Level.Length > 0)
            {
                levelInfo.Sequence = Int32.Parse(Level);
                jsInfo.JobSiteLevels = levelInfo;
            }
            return dal.LoadJobSites(jsInfo);
        }
        public DataSet LoadJobSiteSearchText(string Types, string Level, string SearchText)
        {
            JobSiteInfo jsInfo = new JobSiteInfo();

            if (Types.Length > 0)
            {
                typeInfo.Sequence = Int32.Parse(Types);
                jsInfo.JobSiteTypes = typeInfo;
            }
            if (Level.Length > 0)
            {
                levelInfo.Sequence = Int32.Parse(Level);
                jsInfo.JobSiteLevels = levelInfo;
            }
            jsInfo.SearchText = SearchText;

            return dal.LoadJobSites(jsInfo);
        }
        public void Insert(string RefNo, string Name, string NameShort, string NameEng
            , string NameEngShort, string GFMIS, string JobDescription, string Remarks
            , Nullable<int> CreatedBy, Nullable<DateTime> StartDate, Nullable<DateTime> EndDate
            , string Address, string District, string City, string Country
            , string PostalCode, string Tel, string Fax, Nullable<int> UpperJobSite, int JobSiteTypes
			, int WorkAreas, int JobSiteLevels, Nullable<int> ExternalJobSite, string Address2, string WebAddress
            , string JobSiteHeadSeq)
        {
            try
            {
                typeInfo.Sequence = JobSiteTypes;
                areaInfo.Sequence = WorkAreas;
                levelInfo.Sequence = JobSiteLevels;
                disInfo.Sequence = District.Length > 0 ? Int32.Parse(District) : 0;
                cityInfo.Sequence = City.Length > 0 ? Int32.Parse(City) : 0;
                couInfo.Sequence = Country.Length > 0 ? Int32.Parse(Country) : 0;
                AddressInfo address = new AddressInfo(Address, PostalCode, Tel, Fax, "", "");

                jobsiteInfo.RefNo = RefNo;
                jobsiteInfo.Name = Name;
                jobsiteInfo.NameShort = NameShort;
                jobsiteInfo.NameEng = NameEng;
                jobsiteInfo.NameEngShort = NameEngShort;
                jobsiteInfo.GFMIS = GFMIS;
                jobsiteInfo.JobDescription = JobDescription;
                jobsiteInfo.Remarks = Remarks;
                jobsiteInfo.CreatedBy = CreatedBy;
                //jobsiteInfo.StartDate = StartDate;
                //jobsiteInfo.EndDate = EndDate;
                jobsiteInfo.Upper = UpperJobSite;
                jobsiteInfo.JobSiteTypes = typeInfo;
                jobsiteInfo.WorkAreas = areaInfo;
                jobsiteInfo.JobSiteLevels = levelInfo;
                jobsiteInfo.External = ExternalJobSite;
                jobsiteInfo.Address = address;
				jobsiteInfo.Districts = disInfo;
				jobsiteInfo.City = cityInfo;
				jobsiteInfo.Countries = couInfo;
                jobsiteInfo.Address2 = Address2;
                jobsiteInfo.WebAddress = WebAddress;
                jobsiteInfo.HeadPosSeq = JobSiteHeadSeq.Length > 0 ? Int32.Parse(JobSiteHeadSeq) : (int?)null;

                dal.Insert(jobsiteInfo);
            }
            catch
            {
                throw new Exception("");
            }
        }
        public void Update(int Sequence, string RefNo, string Name, string NameShort, string NameEng
            , string NameEngShort, string GFMIS, string JobDescription, string Remarks
            , Nullable<int> UpdatedBy, Nullable<DateTime> StartDate, Nullable<DateTime> EndDate
            , string Address, string District, string City, string Country
            , string PostalCode, string Tel, string Fax, Nullable<int> UpperJobSite, int JobSiteTypes
			, int WorkAreas, int JobSiteLevels, Nullable<int> ExternalJobSite, string Address2, string WebAddress
            , string JobSiteHeadSeq)
        {
            try
            {
                typeInfo.Sequence = JobSiteTypes;
                areaInfo.Sequence = WorkAreas;
                levelInfo.Sequence = JobSiteLevels;
                disInfo.Sequence = District.Length > 0 ? Int32.Parse(District) : 0;
                cityInfo.Sequence = City.Length > 0 ? Int32.Parse(City) : 0;
                couInfo.Sequence = Country.Length > 0 ? Int32.Parse(Country) : 0;
                AddressInfo address = new AddressInfo(Address, PostalCode, Tel, Fax, "", "");

                jobsiteInfo.Sequence = Sequence;
                jobsiteInfo.RefNo = RefNo;
                jobsiteInfo.Name = Name;
                jobsiteInfo.NameShort = NameShort;
                jobsiteInfo.NameEng = NameEng;
                jobsiteInfo.NameEngShort = NameEngShort;
                jobsiteInfo.GFMIS = GFMIS;
                jobsiteInfo.JobDescription = JobDescription;
                jobsiteInfo.Remarks = Remarks;
                jobsiteInfo.UpdatedBy = UpdatedBy;
                //jobsiteInfo.StartDate = StartDate;
                //jobsiteInfo.EndDate = EndDate;
                jobsiteInfo.Upper = UpperJobSite;
                jobsiteInfo.JobSiteTypes = typeInfo;
                jobsiteInfo.WorkAreas = areaInfo;
                jobsiteInfo.JobSiteLevels = levelInfo;
                jobsiteInfo.External = ExternalJobSite;
                jobsiteInfo.Address = address;
                jobsiteInfo.Districts = disInfo;
                jobsiteInfo.City = cityInfo;
				jobsiteInfo.Countries = couInfo;
				jobsiteInfo.Address2 = Address2;
				jobsiteInfo.WebAddress = WebAddress;
                jobsiteInfo.HeadPosSeq = JobSiteHeadSeq.Length > 0 ? Int32.Parse(JobSiteHeadSeq) : (int?)null;

                dal.Update(jobsiteInfo);
            }
            catch (Exception err)
            {
                // Please check database table "hrm_mp_job_sites"
                // Add Column address2 (varchar 255)
                // Add Column webaddress (varchar 255)
                // Add Column job_site_head_seq (int 11)
            }
        }
        public void Update(JobSiteInfo jobSiteInfoParam)
        {
            try
            {
                // Calculate job_site_full_path
                // Sample#1 => /1/  --> length = 3
                // Sample#2 => /1/3/  --> length = 4, itself index = length -2, parent index = length - 3
                // Sample#3 => /1/3/4/5/6/
                // Sample#4 => /1/3/4/5/6/7/
                var fullPathArr = jobSiteInfoParam.FullPath.Split('/');
                if(fullPathArr.Length > 3)
                {
                    // update parent, copy valid parent from upper
                    fullPathArr[fullPathArr.Length - 3] = Convert.ToString(jobSiteInfoParam.Upper);

                    // join array to string with '/'  
                    var builder = new StringBuilder();
                    builder.Append('/');
                    foreach (string value in fullPathArr)
                    {
                        if (!String.IsNullOrEmpty(value)) // Exclude first and last (empty)
                        {
                            builder.Append(value);
                            builder.Append('/');
                        }
                    } 

                    // save to class
                    jobSiteInfoParam.FullPath = builder.ToString();
                }

                // save to databaes
                dal.Update(jobSiteInfoParam);
            }
            catch (Exception err)
            {
                // Please check database table "hrm_mp_job_sites"
                // Add Column address2 (varchar 255)
                // Add Column webaddress (varchar 255)
                // Add Column job_site_head_seq (int 11)
            }
        } 
        public void Delete(int Sequence, int UpdatedBy)
        {
            try
            {
                dal.Delete(Sequence, UpdatedBy);
            }
            catch
            {
                throw new Exception("");
            }
        }
        
    }
}
